var PENDING = false;
$.fn.serializeObject = function(){
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
function addprojectplanner(){
	if(PENDING)return;
	if( $('#moodycrabprojectplanner').validationEngine('validate') === false)return false;
	var Params = $('#moodycrabprojectplanner').serializeObject();
	PENDING=true;
	jQuery.ajax({
		type: "POST",
		//cache:false,
		//timeout:10000,
		data: {
			Params:Params
		},
		dataType: 'json',
		url: '/home/addprojectplanner',
		success: function(data_result) {
			PENDING = false;
			if(data_result.result>0){
				document.moodycrabprojectplanner.reset();
			}
			$('#moodycrabprojectplanner-message').html('\
				<div class="alert alert-danger fade in">\
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>\
					'+data_result.message+'\
				</div>');
			
		},
		error: function(xhr, ajaxOptions, thrownError) {
			PENDING = false;
			$('#moodycrabprojectplanner-message').html('\
					<div class="alert alert-danger fade in">\
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>\
						Sorry. Your request could not be completed.<br/> Please check your input data and try again.\
					</div>');
		}
	});
}
function newcontactus(){
	if(PENDING)return;
	
	if( $('#moodycrabcontactus').validationEngine('validate') === false)return false;
	var Params = $('#moodycrabcontactus').serializeObject();
	PENDING=true;
	jQuery.ajax({
		type: "POST",
		//cache:false,
		//timeout:10000,
		data: {
			Params:Params
		},
		dataType: 'json',
		url: '/home/addcontacus',
		success: function(data_result) {
			PENDING = false;
			if(data_result.result>0){
				document.moodycrabcontactus.reset();
			}
			$('#contactus-message').html('\
				<div class="alert alert-danger fade in">\
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>\
					'+data_result.message+'\
				</div>');
			
		},
		error: function(xhr, ajaxOptions, thrownError) {
			PENDING = false;
			$('#contactus-message').html('\
					<div class="alert alert-danger fade in">\
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>\
						Sorry. Your request could not be completed.<br/> Please check your input data and try again.\
					</div>');
		}
	});
}
$(document).ready(function(){
	$("#moodycrabcontactus").validationEngine(); 
	$("#moodycrabprojectplanner").validationEngine('attach',{validateNonVisibleFields:true}); 
	$('.project-planner .btn.btn-block').click(function(){
		$('.project-planner .btn.btn-block.active').removeClass('active');
		$(this).addClass('active');
		$('input[name="What"]').val($(this).html());
	});
});